﻿using System.Diagnostics;
using System.Windows;
using HslCommunication.LogNet;
using HslCommunication.Profinet.Siemens;

namespace HslTest;

/// <summary>
///     Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private SiemensS7Net siemensTcpNet;

    public MainWindow()
    {
        InitializeComponent();
        foo();
    }

    ~MainWindow()
    {
        siemensTcpNet.ConnectClose();
    }

    private void foo()
    {
        siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1200, "192.168.1.222")
        {
            ConnectTimeOut = 2000
        };
        siemensTcpNet.LogNet = new LogNetSingle("PLCLog.txt");
    }

    private void BtnConnect_OnClick(object sender, RoutedEventArgs e)
    {
        var connect = siemensTcpNet.ConnectServer();
        MessageBox.Show(connect.IsSuccess ? "连接成功！" : "连接失败！");
    }

    private void BtnDisconnect_OnClick(object sender, RoutedEventArgs e)
    {
        siemensTcpNet.ConnectClose();
    }

    private void BtnWrite_OnClick(object sender, RoutedEventArgs e)
    {
        for (var i = 0; i < 10000; i++)
        {
            // siemensTcpNet.Write("M100.7", true); // 写位，注意M100.0等同于M100
            // siemensTcpNet.Write("M101", 33); // 写单个字节
            // siemensTcpNet.Write("M100", (short)12345); // 写双字节有符号
            // siemensTcpNet.Write("M100", (ushort)45678); // 写双字节无符号
            // siemensTcpNet.Write("M100", 123456789); // 写双字有符号
            // siemensTcpNet.Write("M100", 3456789123); // 写双字无符号
            // siemensTcpNet.Write("M100", 123.456f); // 写单精度
            // siemensTcpNet.Write("M100", 1234556434534545L); // 写大整数有符号
            // siemensTcpNet.Write("M100", 523434234234343UL); // 写大整数无符号
            // siemensTcpNet.Write("M100", 123.456d); // 写双精度
            siemensTcpNet.Write("DB6.16", $"string {i}"); // 写ASCII字符串
            // siemensTcpNet.Write("M101",(int)i); // 写双字有符号
            Debug.WriteLine($"写入次数 {i}");
        }
    }

    private void BtnRead_OnClick(object sender, RoutedEventArgs e)
    {
        for (var i = 0; i < 10000; i++)
        {
            // 读取操作，这里的M100可以替换成I100,Q100,DB20.100效果时一样的
            // var M100_7 = siemensTcpNet.ReadBool("M100.7").Content; // 读取M100.7是否通断，注意M100.0等同于M100
            // var byte_M100 = siemensTcpNet.ReadByte("M100").Content; // 读取M100的值
            // var short_M100 = siemensTcpNet.ReadInt16("M100").Content; // 读取M100-M101组成的字
            // var ushort_M100 = siemensTcpNet.ReadUInt16("M100").Content; // 读取M100-M101组成的无符号的值
            var int_M100 = siemensTcpNet.ReadInt32("M105").Content; // 读取M100-M103组成的有符号的数据
            Debug.WriteLine($"写入次数 {i}: {int_M100}");
            // var uint_M100 = siemensTcpNet.ReadUInt32("M100").Content; // 读取M100-M103组成的无符号的值
            // var float_M100 = siemensTcpNet.ReadFloat("M100").Content; // 读取M100-M103组成的单精度值
            // var long_M100 = siemensTcpNet.ReadInt64("M100").Content; // 读取M100-M107组成的大数据值
            // var ulong_M100 = siemensTcpNet.ReadUInt64("M100").Content; // 读取M100-M107组成的无符号大数据
            // var double_M100 = siemensTcpNet.ReadDouble("M100").Content; // 读取M100-M107组成的双精度值
            // var str_M100 = siemensTcpNet.ReadString("M100", 10).Content; // 读取M100-M109组成的ASCII字符串数据
        }
    }
}